﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace X3dTest
{
    public class X3DDocModel
    {
        private  XmlDocument x3dDoc;// = new XmlDocument();
        private  string x3dmodel;
        //private XmlElement element;
        //private XmlNode rootNode;
        //private XmlNode subNode;


        public X3DDocModel()
        {
            this.x3dDoc = new XmlDocument();
            //x3dmodel = "<?xml version='1.0' encoding='UTF-8'?>\n"
            //+ "<!DOCTYPE X3D PUBLIC 'ISO//Web3D//DTD X3D 3.0//EN' 'http://www.web3d.org/specifications/x3d-3.0.dtd'>\n"
            //+ "<X3D profile='Immersive' version='3.0'>\n"
            //+ "<head>\n"
            //+ "</head>\n"
            //+ "</X3D>\n";
            
            //x3dDoc.LoadXml(x3dmodel);

            //生成描述版本
            XmlDeclaration xmlDec = x3dDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
            x3dDoc.InsertBefore(xmlDec, x3dDoc.DocumentElement);

            XmlNode docType = x3dDoc.CreateNode(XmlNodeType.DocumentType, "X3D", "");
            x3dDoc.AppendChild(docType);

            XmlElement x3dElement = x3dDoc.CreateElement("X3D");
            x3dElement.SetAttribute("profile", "Immersive");
            x3dElement.SetAttribute("version", "3.0");

            XmlNode headNode = x3dDoc.CreateNode(XmlNodeType.Element,"head","");

            XmlElement meta1 = x3dDoc.CreateElement("META");
            meta1.SetAttribute("HTTP-EQUIV", "pragma");
            meta1.SetAttribute("CONTENT", "no-cache");
            headNode.AppendChild((XmlNode)meta1);

            XmlElement meta2 = x3dDoc.CreateElement("META");
            meta2.SetAttribute("HTTP-EQUIV", "Cache-Control");
            meta2.SetAttribute("CONTENT", "no-cache, must-revalidate");
            headNode.AppendChild((XmlNode)meta2);

            XmlElement meta3 = x3dDoc.CreateElement("META");
            meta3.SetAttribute("HTTP-EQUIV", "expires");
            meta3.SetAttribute("CONTENT", "Wed, 26 Feb 1997 08:21:57 GMT");
            headNode.AppendChild((XmlNode)meta3);

            x3dElement.AppendChild(headNode);
            XmlNode sceneNode = x3dDoc.CreateNode(XmlNodeType.Element, "Scene", "");
            x3dElement.AppendChild(sceneNode);
            x3dDoc.AppendChild(x3dElement);           

        }

        public X3DDocModel(string x3dmodel)
        {
            this.x3dmodel = x3dmodel;
            this.x3dDoc = new XmlDocument();
            x3dDoc.LoadXml(this.x3dmodel);
        }

        //public XmlElement CreateElement(string elementName, string attributeName, string attributeValue)
        public XmlElement CreateElement(string elementName)
        {
            XmlElement element = this.x3dDoc.CreateElement(elementName);
            //element.SetAttribute(attributeName, attributeValue);
            return element;
        }

        public XmlNode CreateNode(XmlNodeType type, string nodeName, string namespaceURL)
        {
            XmlNode node = this.x3dDoc.CreateNode(type,nodeName,namespaceURL);
            return node;
        }
        
        public XmlNode AppendChild(string xpath, XmlNode newnode)
        {
            return this.x3dDoc.SelectSingleNode(xpath).AppendChild(newnode);
            //return this.x3ddoc;
        }

        public Boolean AppendChild(string x3dStr)
        {
            XmlDocument tempDoc = new XmlDocument();
            tempDoc.LoadXml(x3dStr);
            x3dDoc.SelectSingleNode("//Scene").AppendChild(x3dDoc.ImportNode(tempDoc.FirstChild,true));
            return true;
        }

        public void saveX3DDoc(string path)
        {
            this.x3dDoc.Save(path);
        }

        public string toString()
        {
            return  this.x3dDoc.OuterXml;
        }


        //用法举例

        //XmlDocModel xmldoc = new XmlDocModel();
        ////XmlNode xmlnode = //(XmlNode)new Scene();
        //XmlElement inlineElement;
        //XmlNode sceneNode;
        //XmlNode transformNode;

        //sceneNode = xmldoc.CreateNode(XmlNodeType.Element,"Scene","");
        //transformNode = xmldoc.CreateNode(XmlNodeType.Element, "Transform", "");

        ////XmlNode subNode;
        //inlineElement = xmldoc.CreateElement("inline");
        //inlineElement.SetAttribute("URL", "ATMS123");
        ////element.SetAttribute("Ver", "1.0");
        //transformNode.AppendChild(inlineElement);
        //sceneNode.AppendChild(transformNode);
        //////rootNode = xmlDoc.AppendChild(element);
        //xmldoc.AppendChild("//X3D",sceneNode);
        //MessageBox.Show(xmldoc.toString());
    }
}
